package com.ideabove.heart.game.character;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;

import com.ideabove.heart.data.Comment;
import com.ideabove.heart.game.StoryGirlState;
import com.ideabove.heart.game.framework.Sprite;
import com.ideabove.heart.manager.MoeManager;
import com.ideabove.heart.manager.ScheduleManager;
import com.ideabove.heart.util.LogUtil;
import com.ideabove.heart.util.StringUtil;


public class EpPlayGirl  extends BasicGirl{

	
	public EpPlayGirl(Comment chInfo) {
		super(chInfo);		
	}
	
	public void loadResource(){
		
		if(ScheduleManager.isTextOnOff((Activity)MoeManager.mCurContext)){
			isTextShow = true;
		}else{
			isTextShow = false;
		}
		
		Comment dressr = ScheduleManager.getDefaultDress((Activity)MoeManager.mCurContext);
//		String defaultDress = "dress_training.png";
//		String defaultHair = "hair_normal.png";
		
		String defaultDress = dressr.getDress();
		String defaultHair = dressr.getHair();
		
		String talkBox = "talk_box.png";
				
		MoeManager.addImg(talkBox);
		MoeManager.addImg(chInfo.getBg());
		MoeManager.addImgs(chInfo.getFace());
		MoeManager.addImg(defaultDress);
		MoeManager.addImg(defaultHair);
		
//		if("df.png".equals(chInfo.getDress())){
//			MoeManager.addImg(defaultDress);
//		}else{
//			MoeManager.addImg(chInfo.getDress());
//			defaultDress = chInfo.getDress();
//		}
//		
//		if("df.png".equals(chInfo.getHair())){
//			MoeManager.addImg(defaultHair);
//		}else{
//			MoeManager.addImg(chInfo.getHair());
//			defaultHair = chInfo.getHair();		
//		}
		
		bg = MoeManager.getImg(chInfo.getBg());
		dress = MoeManager.getImg(defaultDress);
		
		
		face = MoeManager.getImgs(chInfo.getFace());
		mNoOfFrames = face.size(); // 다시 돌리는 구간.		
		
		hair = MoeManager.getImg(defaultHair);
		talkbox = MoeManager.getImg(talkBox);


		if(bg != null){
			scaleX = MoeManager.screenWidth / bg.getWidth();
	 		scaleY = MoeManager.screenHeight / bg.getHeight();		
		    
		    // talkBox	    
		    float talkScaleX = MoeManager.screenWidth / MoeManager.DEFAULT_SC_WIDTH;
		    float talkScaleY = MoeManager.screenHeight / MoeManager.DEFAULT_SC_HEIGHT;
		    
			
			Paint pnt = new Paint();
		    pnt.setColor(Color.WHITE);
	//	    pnt.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
	//	    pnt.setTypeface(Typeface.NORMAL);
		    float fontSize = 21  * talkScaleX;
		    
//		    if(fontSize > 32){
//		    	fontSize = 32;
//		    }
		    
		    pnt.setTextSize(fontSize);	   
//		    pnt.setTextScaleX(MoeManager.screenWidth / MoeManager.DEFAULT_SC_WIDTH);
		    
		    talkX = 18;
		    talkY = (int)(MoeManager.screenHeight - talkbox.getHeight() * talkScaleY) ;
		    StringUtil st = new StringUtil();
			talk = st.setTextInfo(chInfo.getComment(), (int)(MoeManager.screenWidth - (40 * talkScaleX) ), 200, (int)(fontSize) + 2, pnt);
			
			talkSRect = new Rect(0,0,talkbox.getWidth(), talkbox.getHeight());
			
	//		if(st.textBoxHeight + 10 > talkbox.getHeight()){
	//			talkDesRect = new Rect(0,talkY - 80, 10 + (int)(talkbox.getWidth() * scaleX), talkY + (int)(st.textBoxHeight * scaleY) );
	//		}else{
	//			talkDesRect = new Rect(0,talkY - 80, 10 + (int)(talkbox.getWidth() * scaleX), talkY + talkbox.getHeight());
	//		}	
			
			talkDesRect = new Rect(0,talkY - 80, 10 + (int)(talkbox.getWidth() * talkScaleX), talkY + (int)(talkbox.getHeight() * talkScaleY));
		}
	}
	
	
	public void init(){

		AssetFileDescriptor afd;
		try {
			afd = MoeManager.mCurContext.getAssets().openFd("voice/" + chInfo.getVoice());
			mPlayer = new MediaPlayer();
			mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
			afd.close();
			mPlayer.prepare();
			mPlayer.start();
			
			float volume = ScheduleManager.getCurrentVoiceVolume((Activity)MoeManager.mCurContext);
			mPlayer.setVolume(volume, volume);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			((Activity)MoeManager.mCurContext).finish();
			e.printStackTrace();		
		}	
	
	}	
	
	public void repeatVoice(){
		if(mPlayer != null){
			mPlayer.pause();
			mPlayer.seekTo(0);
			mPlayer.start();
		}
	}
}
